package com.paireach.bw.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.utils.NetworkUtils;
import com.paireach.bw.R;
import com.paireach.bw.adapter.CommonLoadAdapter;
import com.paireach.bw.application.MyApplication;
import com.paireach.bw.base.BaseFragment;
import com.paireach.bw.bean.DriveOrder;
import com.paireach.bw.bean.LocationInfo;
import com.paireach.bw.bean.OrderResult;
import com.paireach.bw.bean.OrdersBean;
import com.paireach.bw.bean.ReturnInfo;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.http.RequestTag;
import com.paireach.bw.http.WayBillRequest;
import com.paireach.bw.utils.GsonUtil;
import com.paireach.bw.view.CustomDividerItemDecoration;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by ${cqc} on 2017/6/19.
 */

public class CommonFrag226 extends BaseFragment {

    private View rootView;
    private Toolbar toolbar;
    private RecyclerView recyclerView;

    private List<OrdersBean> orderList;
    private String orderCode;

    public static CommonFrag226 getInstance( OrderResult orderResult) {
        CommonFrag226 frag = new CommonFrag226();
        Bundle bundle = new Bundle();
        bundle.putSerializable("KaOrderResult", orderResult);
        frag.setArguments(bundle);
        return frag;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        OrderResult orderResult = (OrderResult) bundle.getSerializable("KaOrderResult");
        orderList = orderResult.getOrders();
        orderCode = orderList.get(0).getORDER_CODE();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.frag_common_load, container, false);
        findViews();
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViews();
    }


    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        dismissProgressDialog();
        if (event.getRequestTag() == RequestTag.INTO_FACTORY) {
            ReturnInfo info = GsonUtil.jsonToBean(event.getJson(), ReturnInfo.class);
            showToast(info.getMsg());
            if (info.getStatus() == 1) {
                AppEvent appEvent = new AppEvent();
                appEvent.setTag("GetOrderCodeDetail");//通知CommonActivity页面请求数据，然后更新fragment
                EventBus.getDefault().post(appEvent);
            }
        }
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        dismissProgressDialog();
        if (event.getRequestTag() == RequestTag.INTO_FACTORY) {
            showToast(event.getErrorMessage());
            AppEvent appEvent = new AppEvent();
            appEvent.setTag("加载失败");
            EventBus.getDefault().post(appEvent);
        }
    }


    private void initViews() {
        toolbar.setTitle("待入厂");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().onBackPressed();
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
//        recyclerView.addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.VERTICAL));
        recyclerView.addItemDecoration(new CustomDividerItemDecoration(context, CustomDividerItemDecoration.VERTICAL));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        CommonLoadAdapter adapter = new CommonLoadAdapter(orderList);
        recyclerView.setAdapter(adapter);

        View headView = LayoutInflater.from(context).inflate(R.layout.header_common_load, recyclerView, false);
        adapter.addHeaderView(headView);
        initHeadViews(headView);

        View footView = LayoutInflater.from(context).inflate(R.layout.footer_ka_common, recyclerView, false);
        adapter.addFooterView(footView);
        initFootView(footView);
    }

    private void initFootView(View footView) {
        TextView tv_footer = (TextView) footView.findViewById(R.id.tv_footer);
        tv_footer.setText("入厂确认");
        tv_footer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NetworkUtils.isConnected()) {
                    showProgressDialog();
                    LocationInfo location = ((MyApplication) getActivity().getApplication()).locationInfo;
                    new WayBillRequest().intoFactory(phoneNumber, orderCode, location.getLongitude() + "", location.getLatitude() + "", driverNum);
                } else {
                    showToast("网络连接不可用，请稍后重试");
                }
            }
        });
    }

    private void initHeadViews(View headView) {
        TextView tv_tip = (TextView) headView.findViewById(R.id.tv_tip);
        TextView tv_order_num_parent = (TextView) headView.findViewById(R.id.tv_order_num_parent);
        TextView tv_origin_address_parent = (TextView) headView.findViewById(R.id.tv_origin_address_parent);
        TextView tv_weight = (TextView) headView.findViewById(R.id.tv_weight);
        TextView tv_contacts_parent = (TextView) headView.findViewById(R.id.tv_contacts_parent);
        TextView tv_contacts_tel_parent = (TextView) headView.findViewById(R.id.tv_contacts_tel_parent);

        tv_tip.setText(getResources().getString(R.string.common_tip_226));
        OrdersBean info = orderList.get(0);
        tv_order_num_parent.setText("负载单号：" + info.getORDER_CODE());
        tv_origin_address_parent.setText("发货地址：" + info.getFRM_SHPG_ADDR());
        tv_weight.setText("货物重量：" + info.getBW_WGT() + "kg");
        tv_contacts_parent.setText("联系人：" + info.getDRIVER_NAME());
        tv_contacts_tel_parent.setText("电话：" + info.getDRIVER_MOBILE());
    }

    private void findViews() {
        toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
    }

}
